package com.yanqu.road.dataHelper.action.zhounianqing;

import com.yanqu.road.dataHelper.action.excel.ZhouNianQingRankData;
import com.yanqu.road.dataHelper.action.excel.ZhouNianRankLogic;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.player.UserAttribute;
import com.yanqu.road.entity.servercenter.ServerInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class BanquetDataETLCaller implements Callable<List<ZhouNianQingRankData>> {

    private List<Integer> eventType;

    private int rankType;

    ServerInfo serverInfo;

    int size;

    public BanquetDataETLCaller(List<Integer> eventType, int rankType, ServerInfo serverInfo, int size) {
        this.eventType = eventType;
        this.rankType = rankType;
        this.serverInfo = serverInfo;
        this.size = size;
    }

    @Override
    public List<ZhouNianQingRankData> call() throws Exception {
        GameServer testGameServer = new GameServer(serverInfo);
        ZhouNianRankLogic resource = new ZhouNianRankLogic(testGameServer);
        //List<ZhouNianQingRankData> serverRankList = resource.getServerRankList(serverInfo.getServerId(), eventType, size, rankType);

        List<ZhouNianQingRankData> serverRankList = new ArrayList<>();
        //宴会冲榜
        //本服    1       10
        //跨服    12       5
        ServerMgr serverMgr = new ServerMgr();
        Map<Integer, ActivityInfo> activityInfoMap = serverMgr.getActivityInfoBy(1, 10, 12, 5, serverInfo.getServerId(), "2021-02-25 00:00:00", "2022-08-15 00:00:00");
        if(activityInfoMap.size() > 0){
            List<Integer> activityIdList = new ArrayList<>();
            for (int id : activityInfoMap.keySet()) {
                activityIdList.add(id);
            }
            serverRankList = resource.getServerRankListByActivityId(serverInfo.getServerId(), activityIdList, size, rankType);
        }

        if(serverRankList.size() > 0) {
            //填充活动数据
            //resource.fillActivityInfo(serverRankList);
            for (ZhouNianQingRankData rankData : serverRankList) {
                if(activityInfoMap.containsKey(rankData.getActivityId())) {
                    rankData.setDataTime(activityInfoMap.get(rankData.getActivityId()).getEndTime());
                }
            }
        }else {
            System.out.println("区服没有宴会冲榜：" + serverInfo.getServerId());
            return serverRankList;
        }
        //2020-07-15 00:00:00      1594742400
        //2020-09-01 00:00:00      1598889600
        //2021-07-16 00:00:00      1626364800


        long beginTime = 1614178800 * 1000L;
        long endTime = 1660492800 * 1000L;


        //宴会数据
        //宴会总人气|跨服宴会场次|本服宴会场次|普通宴会场次
        for (ZhouNianQingRankData rankData : serverRankList) {
            //总人气
            long banquetPopularity = 0;
            UserAttribute attribute = resource.getUserAttribute(rankData.getUserId(), ePlayerAttrType.BanquetPopularity.getValue());
            if(attribute != null){
                banquetPopularity = attribute.getValue();
            }

            //跨服数据库宴会场次
            Map<Long, List<BanquetCount>> crossServerDataMap = serverMgr.getCrossBanquet(rankData.getUserId(), beginTime, endTime);

            //跨服数据库宴会场次-私宴      当区服宴会
            Map<Long, List<BanquetCount>> crossServerPersonalDataMap = serverMgr.getCrossBanquetPersonal(rankData.getUserId(), beginTime, endTime);

            //区服数据库-本服宴会场次
            Map<Long, List<BanquetCount>> serverDataMap = resource.getServerBanquet(rankData.getUserId(), beginTime, endTime);

            //区服数据库-跨服宴会场次
            Map<Long, List<BanquetCount>> crossServerDataMap2 = resource.getCrossServerBanquet(rankData.getUserId(), beginTime, endTime);


            //跨服宴会场次(40001\40002\40003)
            int crossNum = 0;
            //区服宴会场次(30001\30002\30003)
            int serverNum = 0;
            //普通宴会场次(其他)
            int normalNum = 0;

            if(crossServerDataMap.containsKey(rankData.getUserId())){
                for (BanquetCount banquetCount : crossServerDataMap.get(rankData.getUserId())) {
                    if(isCrossBanquet(banquetCount)){
                        crossNum += banquetCount.getCount();
                    }else if(isServerBanquet(banquetCount)){
                        serverNum += banquetCount.getCount();
                    }else {
                        normalNum += banquetCount.getCount();
                    }
                }
            }

            if(crossServerDataMap2.containsKey(rankData.getUserId())){
                for (BanquetCount banquetCount : crossServerDataMap2.get(rankData.getUserId())) {
                    if(isCrossBanquet(banquetCount)){
                        crossNum += banquetCount.getCount();
                    }else if(isServerBanquet(banquetCount)){
                        serverNum += banquetCount.getCount();
                    }else {
                        normalNum += banquetCount.getCount();
                    }
                }
            }

            if(serverDataMap.containsKey(rankData.getUserId())){
                for (BanquetCount banquetCount : serverDataMap.get(rankData.getUserId())) {
                    if(isCrossBanquet(banquetCount)){
                        crossNum += banquetCount.getCount();
                    }else if(isServerBanquet(banquetCount)){
                        serverNum += banquetCount.getCount();
                    }else {
                        normalNum += banquetCount.getCount();
                    }
                }
            }

            if(crossServerPersonalDataMap.containsKey(rankData.getUserId())){
                for (BanquetCount banquetCount : crossServerPersonalDataMap.get(rankData.getUserId())) {
                    if(isCrossBanquet(banquetCount)){
                        crossNum += banquetCount.getCount();
                    }else if(isServerBanquet(banquetCount)){
                        serverNum += banquetCount.getCount();
                    }else {
                        normalNum += banquetCount.getCount();
                    }
                }
            }

            rankData.setOtherValue(banquetPopularity + ZhouNianQingRankData.OTHER_VALUE_SEPARATOR + crossNum + ZhouNianQingRankData.OTHER_VALUE_SEPARATOR + serverNum + ZhouNianQingRankData.OTHER_VALUE_SEPARATOR + normalNum);
        }
        return serverRankList;
    }

    private boolean isServerBanquet(BanquetCount banquetCount) {
        return banquetCount.getType() > 30000 && banquetCount.getType() < 40000;
    }

    private boolean isCrossBanquet(BanquetCount banquetCount) {
        return banquetCount.getType() > 40000 && banquetCount.getType() < 50000;
    }
}
